package pac;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import BackEnd.Folio;
import BackEnd.IFolio;

public class login extends JFrame implements ActionListener, WindowListener,
		ILogin {

	private JPanel contentPane;
	private JTextField userNameBox;
	private JTextField passWordBox;
	boolean alreadyuser = false;
	IFolio folio = new Folio();
	StringTokenizer split;
	String userName;

	public login() {
		setTitle("Log In");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 270, 196);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblStockTicker = new JLabel("Username:");
		lblStockTicker.setBounds(10, 24, 156, 14);
		contentPane.add(lblStockTicker);

		userNameBox = new JTextField();
		userNameBox.setBounds(129, 21, 115, 20);
		contentPane.add(userNameBox);
		userNameBox.setColumns(10);

		JButton btnAdd = new JButton("Log in");

		userName = userNameBox.getText().toString();

		btnAdd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String userN = userNameBox.getText();
				String passW = passWordBox.getText();

				if (userN.isEmpty() || passW.isEmpty()) {
					System.out.println("try");
				} else {

					try {
						// command line parameter
						BufferedReader br = new BufferedReader(new FileReader(
								"login.txt"));

						String str;
						while ((str = br.readLine()) != null) {
							split = new StringTokenizer(str);
							String fileUsern = split.nextToken(" ");
							String filePass = split.nextToken();

							if (fileUsern.equals(userN)) {
								if (filePass.equals(passW)) {
									br.close();
									try {
										BufferedWriter out = new BufferedWriter(
												new FileWriter("cheat.txt"));
										out.write(userN);
										out.close();
									} catch (IOException e5) {
										System.out.println("Exception ");

									}

									frame main = new frame(folio);
									main.setVisible(true);
									main.addWindowListener(main);
									dispose();
								}
							}
						}
					} catch (IOException e1) {
					}
				}
			}
		});
		btnAdd.setBounds(20, 83, 89, 23);

		contentPane.add(btnAdd);

		JButton btnCancel = new JButton("Exit");
		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// quit
				System.exit(0);

			}
		});
		btnCancel.setBounds(142, 83, 89, 23);
		contentPane.add(btnCancel);

		JLabel lblPassword = new JLabel("Password:");
		lblPassword.setBounds(10, 49, 69, 14);
		contentPane.add(lblPassword);

		passWordBox = new JTextField();
		passWordBox.setBounds(129, 49, 115, 20);
		contentPane.add(passWordBox);
		passWordBox.setColumns(10);

		JButton btnNewUser = new JButton("New User");
		btnNewUser.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String userN = userNameBox.getText();
				String passW = passWordBox.getText();

				try {
					BufferedReader br = new BufferedReader(new FileReader(
							"login.txt"));

					String str;
					while ((str = br.readLine()) != null) {
						split = new StringTokenizer(str);
						String fileUsern = split.nextToken(" ");
						String filePass = split.nextToken();

						if (fileUsern.equals(userN)) {
						alreadyuser = true;	
						}
					}
				} catch (Exception e2) {
					// TODO: handle exception
				}
				if(alreadyuser == false){ 
				if (userN.isEmpty() || passW.isEmpty()) {
					System.out.println("try");
				} else {

					PrintWriter out = null;
					try {
						out = new PrintWriter(new FileWriter("login.txt", true));
						String userInfo = userNameBox.getText() + " "
								+ passWordBox.getText();
						out.println(userInfo);

					} catch (IOException e2) {
						System.out.println("IOException: Please Retry");
					} finally {
						out.close();
						// Creates the new user and opens a new folio.
						String fileName = userNameBox.getText() + ".txt";
						File f = new File(fileName);
						if (!f.exists()) {
							try {
								f.createNewFile();
							} catch (IOException e3) {
								System.out.println("Unkown error cannot create file!");
							}
						}
						try {
							BufferedWriter out1 = new BufferedWriter(
									new FileWriter("cheat.txt"));
							out1.write(userNameBox.getText());
							out1.close();
						} catch (IOException e5) {
							System.out.println("Exception ");

						}
						frame main = new frame(folio);
						main.setVisible(true);
						main.addWindowListener(main);
						dispose();

					}
				}
			}
			}
		});
		btnNewUser.setBounds(77, 117, 89, 23);
		contentPane.add(btnNewUser);
	}

	public void close() {
		this.dispose();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosing(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public String GetName() {
		userName = userNameBox.getText();
		return userName;
	}

}
